package com.example.ff.jobs;

import android.app.job.JobParameters;
import android.app.job.JobService;
import android.content.Context;
import android.content.Intent;
import android.media.MediaPlayer;
import android.media.RingtoneManager;
import android.os.Build;
import android.os.Bundle;
import android.os.Message;
import android.os.Messenger;
import android.os.RemoteException;
import android.os.Vibrator;
import androidx.annotation.RequiresApi;
import android.util.Log;
import android.widget.Toast;

import java.util.Timer;
import java.util.TimerTask;

@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
public class MJobService extends JobService {

    private static final String TAG = "MJobService";

    private Messenger messenger;
    private JobParameters parameters;

    private static  int value ;



    public boolean someJob(JobParameters params){
        final int id = parameters.getJobId();
        final String data = params.getExtras().getString("JobData");


        int periodic = params.getExtras().getInt("Periodic");
        if (periodic == 1) {
            if (Build.VERSION.SDK_INT < 24) {
                TimerTask task = new TimerTask() {
                    @Override
                    public void run() {
                        Log.d(TAG, "someJob : id = " + id + " data = " + data + " value = " + (++value));
                        Vibrator vibrator = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);
                        long[] pattern = {100, 400, 100, 400};
                        vibrator.vibrate(pattern, -1);
                        if (value == 20) {
                            jobFinished(parameters, false);
                        }
                    }
                };
                Timer timer = new Timer();
                int period = params.getExtras().getInt("period");
                timer.schedule(task, 0, period * 1000 * 60);
                return true;
            }
        }
        Log.d(TAG, "someJob : id = " + id + " data = " + data + " value = " + (++value) );
        Vibrator vibrator = (Vibrator)getSystemService(Context.VIBRATOR_SERVICE);
        long [] pattern = {100,400,100,400};
        vibrator.vibrate(pattern,-1);
        jobFinished(parameters,false);
        return false;
    }
    @Override
    public boolean onStartJob(final JobParameters params) {
        Log.d(TAG,"onStartJob ");
        parameters = params;
        return someJob(params);
    }

    @Override
    public boolean onStopJob(JobParameters params) {
        Log.d(TAG," onStopJob ");
        jobFinished(params,true);
        Toast.makeText(getApplicationContext(),"Job " + params.getJobId() + " stopped",Toast.LENGTH_SHORT).show();
        return false;
    }

    @Override
    public void onCreate() {
        Log.d(TAG," onCreate ");
        super.onCreate();
    }

    @Override
    public void onDestroy() {
        Log.d(TAG," onDestroy ");
        super.onDestroy();
    }

    @Override
    public void onTaskRemoved(Intent rootIntent) {
        Log.d(TAG," onTaskRemoved ");
        super.onTaskRemoved(rootIntent);
    }

    @Override
    public void onLowMemory() {
        Log.d(TAG," onLowMemory ");
        super.onLowMemory();
    }

    @Override
    public void onTrimMemory(int level) {
        Log.d(TAG," onTrimMemory ");
        super.onTrimMemory(level);
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        Log.d(TAG," onStartCommand ");

        messenger = intent.getParcelableExtra(MJobConstant.MESSENGER_INTENT_KEY);

        try {
            Bundle data = new Bundle();
            data.putString(MJobConstant.MESSENGER_START_KEY,"MJobService : I'm starting in process [ " + android.os.Process.myPid() + " ] ");

            Message msg = new Message();
            msg.what = MJobConstant.MESSENGER_START;
            msg.setData(data);

            messenger.send(msg);
        } catch (RemoteException e) {
            e.printStackTrace();
        }

        MediaPlayer mp = new MediaPlayer();
        try {
            mp.setDataSource(this, RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION));
            mp.prepare();
            mp.start();
        } catch (Exception e) {
            e.printStackTrace();
        }

        return  START_STICKY;
    }

    @Override
    public boolean onUnbind(Intent intent) {
        Log.d(TAG," onUnbind ");
        return super.onUnbind(intent);
    }

    @Override
    public void onRebind(Intent intent) {
        Log.d(TAG," onRebind ");

        super.onRebind(intent);
    }
}
